

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cortex.plugins &mdash; Cortex2.0  documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 

  
  <script src="../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../index.html" class="icon icon-home"> Cortex2.0
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">User Documentation</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../install.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../getting_started.html">Getting Started</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../modules.html">cortex</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../develop.html">Develop</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../build.html">Custom demos</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../build.html#a-walkthrough-a-custom-classifier">A walkthrough a custom classifier:</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../build.html#defining-losses-and-results">Defining losses and results</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../build.html#visualization">Visualization</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../build.html#putting-it-together">Putting it together</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">Cortex2.0</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>cortex.plugins</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cortex.plugins</h1><div class="highlight"><pre>
<span></span><span class="sd">&#39;&#39;&#39;Module for plugins</span>

<span class="sd">&#39;&#39;&#39;</span>

<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">from</span> <span class="nn">os</span> <span class="k">import</span> <span class="n">path</span>
<span class="kn">import</span> <span class="nn">shutil</span>

<span class="kn">from</span> <span class="nn">torch.utils.data</span> <span class="k">import</span> <span class="n">Dataset</span>

<span class="kn">from</span> <span class="nn">cortex._lib.config</span> <span class="k">import</span> <span class="n">CONFIG</span><span class="p">,</span> <span class="n">_config_name</span>
<span class="kn">from</span> <span class="nn">cortex._lib.data</span> <span class="k">import</span> <span class="n">DatasetPluginBase</span><span class="p">,</span> <span class="n">register</span> <span class="k">as</span> <span class="n">register_data</span>
<span class="kn">from</span> <span class="nn">cortex._lib.models</span> <span class="k">import</span> <span class="n">ModelPluginBase</span><span class="p">,</span> <span class="n">register_model</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s1">&#39;R Devon Hjelm&#39;</span>
<span class="n">__author_email__</span> <span class="o">=</span> <span class="s1">&#39;erroneus@gmail.com&#39;</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;DatasetPlugin&#39;</span><span class="p">,</span>
    <span class="s1">&#39;ModelPlugin&#39;</span><span class="p">,</span>
    <span class="s1">&#39;register_plugin&#39;</span><span class="p">]</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;cortex.plugins&#39;</span><span class="p">)</span>


<div class="viewcode-block" id="DatasetPlugin"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.DatasetPlugin">[docs]</a><span class="k">class</span> <span class="nc">DatasetPlugin</span><span class="p">(</span><span class="n">DatasetPluginBase</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;Basic plugin class for datasets into cortex</span>

<span class="sd">    Attributes:</span>
<span class="sd">        sources: list of dataset string names that this plugin will support.</span>

<span class="sd">    &#39;&#39;&#39;</span>
    <span class="n">sources</span> <span class="o">=</span> <span class="p">[]</span>

<div class="viewcode-block" id="DatasetPlugin.copy_to_local_path"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.DatasetPlugin.copy_to_local_path">[docs]</a>    <span class="k">def</span> <span class="nf">copy_to_local_path</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">from_path</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span><span class="p">:</span>

        <span class="sd">&#39;&#39;&#39; Copies data to a local path.</span>

<span class="sd">        Path is set in the .cortex.yml file. This can be set up through</span>
<span class="sd">        `cortex setup`.</span>

<span class="sd">        Args:</span>
<span class="sd">            from_path: The path to the data to be copied.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">if</span> <span class="n">from_path</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">):</span>
            <span class="n">from_path</span> <span class="o">=</span> <span class="n">from_path</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">basename</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">from_path</span><span class="p">)</span>
        <span class="n">local_path</span> <span class="o">=</span> <span class="n">CONFIG</span><span class="o">.</span><span class="n">data_paths</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;local&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">local_path</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span>
                <span class="s1">&#39;`</span><span class="si">{}</span><span class="s1">` not found in </span><span class="si">{}</span><span class="s1"> data_paths&#39;</span>
                <span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">local_path</span><span class="p">,</span> <span class="n">_config_name</span><span class="p">))</span>
        <span class="n">to_path</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">local_path</span><span class="p">,</span> <span class="n">basename</span><span class="p">)</span>
        <span class="k">if</span> <span class="p">((</span><span class="ow">not</span> <span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">to_path</span><span class="p">))</span> <span class="ow">and</span> <span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">from_path</span><span class="p">)):</span>

            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Copying dataset </span><span class="si">{}</span><span class="s1"> from </span><span class="si">{}</span><span class="s1"> to </span><span class="si">{}</span><span class="s1"> directory.... &#39;</span>
                        <span class="s1">&#39;(This may take time)&#39;</span>
                        <span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">from_path</span><span class="p">,</span> <span class="n">to_path</span><span class="p">))</span>

            <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">from_path</span><span class="p">):</span>
                <span class="n">shutil</span><span class="o">.</span><span class="n">copytree</span><span class="p">(</span><span class="n">from_path</span><span class="p">,</span> <span class="n">to_path</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">shutil</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">from_path</span><span class="p">,</span> <span class="n">local_path</span><span class="p">)</span>

            <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s1">&#39;Finished copying.&#39;</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">to_path</span></div>

<div class="viewcode-block" id="DatasetPlugin.add_dataset"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.DatasetPlugin.add_dataset">[docs]</a>    <span class="k">def</span> <span class="nf">add_dataset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mode</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">dataset</span><span class="p">:</span> <span class="n">Dataset</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Adds a dataset to the plugin.</span>

<span class="sd">        Any dataset added in this way will be used in the training or testing</span>
<span class="sd">        loops, depending on the mode specified.</span>

<span class="sd">        Args:</span>
<span class="sd">            mode: The data mode that this dataset will be run on.</span>
<span class="sd">                `train` and `test` are highly recommended.</span>
<span class="sd">            dataset: The dataset object.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">if</span> <span class="n">mode</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_datasets</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span>
                <span class="s1">&#39;`</span><span class="si">{}</span><span class="s1">` already added to datasets in entrypoint&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">mode</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_datasets</span><span class="p">[</span><span class="n">mode</span><span class="p">]</span> <span class="o">=</span> <span class="n">dataset</span></div>

<div class="viewcode-block" id="DatasetPlugin.get_path"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.DatasetPlugin.get_path">[docs]</a>    <span class="k">def</span> <span class="nf">get_path</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">source</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Get&#39;s the path to a source.</span>

<span class="sd">        This is derived from config.yaml file.</span>

<span class="sd">        Args:</span>
<span class="sd">            source: str for the dataset source.</span>

<span class="sd">        Returns:</span>
<span class="sd">            The path to the dataset.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">CONFIG</span><span class="o">.</span><span class="n">data_paths</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">source</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">p</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span>
                <span class="s1">&#39;`</span><span class="si">{}</span><span class="s1">` not found in </span><span class="si">{}</span><span class="s1"> data_paths&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">source</span><span class="p">,</span> <span class="n">_config_name</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">p</span></div>

<div class="viewcode-block" id="DatasetPlugin.set_input_names"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.DatasetPlugin.set_input_names">[docs]</a>    <span class="k">def</span> <span class="nf">set_input_names</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">input_names</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Sets the names of the elements of the dataset.</span>

<span class="sd">        For use downstream in models.</span>

<span class="sd">        Args:</span>
<span class="sd">            input_names (:obj:`list` of :obj:`str`): The input names.</span>
<span class="sd">                Should be the same size as the output of the dataset iterator.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_input_names</span> <span class="o">=</span> <span class="n">input_names</span></div>

<div class="viewcode-block" id="DatasetPlugin.set_dims"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.DatasetPlugin.set_dims">[docs]</a>    <span class="k">def</span> <span class="nf">set_dims</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39; Sets the dimenisions of the data</span>

<span class="sd">        Args:</span>
<span class="sd">            **kwargs: a dictionary of dimension keys and ints.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_dims</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span></div>

<div class="viewcode-block" id="DatasetPlugin.set_scale"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.DatasetPlugin.set_scale">[docs]</a>    <span class="k">def</span> <span class="nf">set_scale</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">scale</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Sets the min / max values for the data.</span>

<span class="sd">        Note:</span>
<span class="sd">            This will probably be removed. It doesn&#39;t even function right now.</span>

<span class="sd">        Args:</span>
<span class="sd">            scale (:obj:`tuple` of :obj:`float`): min/max pair.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_scale</span> <span class="o">=</span> <span class="n">scale</span></div>

<div class="viewcode-block" id="DatasetPlugin.make_indexing"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.DatasetPlugin.make_indexing">[docs]</a>    <span class="k">def</span> <span class="nf">make_indexing</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">C</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Makes an indexing dataset.</span>

<span class="sd">        Index comes in as the last element of the batch.</span>

<span class="sd">        Args:</span>
<span class="sd">            C: data.Dataset class.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Wrapped data.Dataset class.</span>

<span class="sd">        &#39;&#39;&#39;</span>

        <span class="k">class</span> <span class="nc">IndexingDataset</span><span class="p">(</span><span class="n">C</span><span class="p">):</span>
            <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">index</span><span class="p">):</span>
                <span class="n">output</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__getitem__</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>
                <span class="k">return</span> <span class="n">output</span> <span class="o">+</span> <span class="p">(</span><span class="n">index</span><span class="p">,)</span>

        <span class="k">return</span> <span class="n">IndexingDataset</span></div></div>


<div class="viewcode-block" id="ModelPlugin"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.ModelPlugin">[docs]</a><span class="k">class</span> <span class="nc">ModelPlugin</span><span class="p">(</span><span class="n">ModelPluginBase</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;Module plugin.</span>

<span class="sd">    Attributes:</span>
<span class="sd">        plugin_name (str): Name of the plugin.</span>
<span class="sd">        data_defaults (:obj:`dict`): Data defaults.</span>
<span class="sd">        train_defaults (:obj:`dict`): Train defaults.</span>
<span class="sd">        optimizer_defaults (:obj:`dict`): Optimizer defaults.</span>

<span class="sd">    &#39;&#39;&#39;</span>
    <span class="n">_protected</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;description&#39;</span><span class="p">]</span>
    <span class="n">_required</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">_optional</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;setup&#39;</span><span class="p">]</span>

    <span class="n">defaults</span> <span class="o">=</span> <span class="p">{}</span>

<div class="viewcode-block" id="ModelPlugin.build"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.ModelPlugin.build">[docs]</a>    <span class="k">def</span> <span class="nf">build</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Builds the neural networks.</span>

<span class="sd">        The the model is to build something, this needs to be overridden.</span>

<span class="sd">        Args:</span>
<span class="sd">            *args: Inputs to be passed to the function.</span>
<span class="sd">            **kwargs: Hyperparameters to be passed to the function</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
            <span class="s1">&#39;`build` is not implemented for model class </span><span class="si">{}</span><span class="s1">&#39;</span>
            <span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span></div>

<div class="viewcode-block" id="ModelPlugin.routine"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.ModelPlugin.routine">[docs]</a>    <span class="k">def</span> <span class="nf">routine</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Derives losses and results.</span>

<span class="sd">            The the model is to train something, this needs to be</span>
<span class="sd">            overridden.</span>

<span class="sd">            Args:</span>
<span class="sd">                *args: Inputs to be passed to the function.</span>
<span class="sd">                **kwargs: Hyperparameters to be passed to the function</span>

<span class="sd">            &#39;&#39;&#39;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span>
            <span class="s1">&#39;`routine` is not implemented for model class </span><span class="si">{}</span><span class="s1">&#39;</span>
            <span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span></div>

<div class="viewcode-block" id="ModelPlugin.visualize"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.ModelPlugin.visualize">[docs]</a>    <span class="k">def</span> <span class="nf">visualize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Visualizes.</span>

<span class="sd">            The the model is to visualize something, this needs to be</span>
<span class="sd">            overridden.</span>

<span class="sd">            Args:</span>
<span class="sd">                *args: Inputs to be passed to the function.</span>
<span class="sd">                **kwargs: Hyperparameters to be passed to the function</span>

<span class="sd">            &#39;&#39;&#39;</span>
        <span class="k">pass</span></div>

<div class="viewcode-block" id="ModelPlugin.train_step"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.ModelPlugin.train_step">[docs]</a>    <span class="k">def</span> <span class="nf">train_step</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Makes a training step.</span>

<span class="sd">        This can be overridden to change the behavior at each training step.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">routine</span><span class="p">(</span><span class="n">auto_input</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">optimizer_step</span><span class="p">()</span></div>

<div class="viewcode-block" id="ModelPlugin.eval_step"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.ModelPlugin.eval_step">[docs]</a>    <span class="k">def</span> <span class="nf">eval_step</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Makes an evaluation step.</span>

<span class="sd">        This can be overridden to change the behavior of each evaluation step.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">routine</span><span class="p">(</span><span class="n">auto_input</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span></div>

<div class="viewcode-block" id="ModelPlugin.optimizer_step"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.ModelPlugin.optimizer_step">[docs]</a>    <span class="k">def</span> <span class="nf">optimizer_step</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Makes a step of the optimizers for which losses are defined.</span>

<span class="sd">        This can be overridden to change the behavior of the optimizer.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="n">keys</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">losses</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">keys</span><span class="p">):</span>
            <span class="n">loss</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">losses</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">k</span><span class="p">)</span>
            <span class="n">loss</span><span class="o">.</span><span class="n">backward</span><span class="p">(</span><span class="n">retain_graph</span><span class="o">=</span><span class="p">(</span><span class="n">i</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">keys</span><span class="p">)))</span>
            <span class="c1">#  TODO(Devon): Is this a good idea?</span>
            <span class="n">key</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">nets</span><span class="o">.</span><span class="n">_aliases</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">k</span><span class="p">)</span>

            <span class="n">optimizer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_optimizers</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">optimizer</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">optimizer</span><span class="o">.</span><span class="n">step</span><span class="p">()</span></div>

<div class="viewcode-block" id="ModelPlugin.train_loop"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.ModelPlugin.train_loop">[docs]</a>    <span class="k">def</span> <span class="nf">train_loop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;The training loop.</span>

<span class="sd">        This can be overridden to change the behavior of the training loop.</span>

<span class="sd">        &#39;&#39;&#39;</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">train_step</span><span class="p">()</span>

        <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span>
            <span class="k">pass</span></div>

<div class="viewcode-block" id="ModelPlugin.eval_loop"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.ModelPlugin.eval_loop">[docs]</a>    <span class="k">def</span> <span class="nf">eval_loop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;The evaluation loop.</span>

<span class="sd">        This can be overridden to change the behavior of the evaluation loop.</span>

<span class="sd">        &#39;&#39;&#39;</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">eval_step</span><span class="p">()</span>

        <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span>
            <span class="k">pass</span></div>

<div class="viewcode-block" id="ModelPlugin.get_dims"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.ModelPlugin.get_dims">[docs]</a>    <span class="k">def</span> <span class="nf">get_dims</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">queries</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Gets dimensions of inputs.</span>

<span class="sd">        Args:</span>
<span class="sd">            *queries: Variables to get dimensions of .</span>

<span class="sd">        Returns:</span>
<span class="sd">            Dimensions of the variables.</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">get_dims</span><span class="p">(</span><span class="o">*</span><span class="n">queries</span><span class="p">)</span></div>

<div class="viewcode-block" id="ModelPlugin.add_noise"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.ModelPlugin.add_noise">[docs]</a>    <span class="k">def</span> <span class="nf">add_noise</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">dist</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Adds a noise variable to the model.</span>

<span class="sd">        Args:</span>
<span class="sd">            key (str): Name of the noise variable.</span>
<span class="sd">            dist (str): Noise distribution.</span>
<span class="sd">            size (int): Size of the noise.</span>
<span class="sd">            **kwargs: keyword arguments for noise distribution.</span>
<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_data</span><span class="o">.</span><span class="n">add_noise</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">dist</span><span class="o">=</span><span class="n">dist</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="n">size</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>

<div class="viewcode-block" id="ModelPlugin.add_image"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.ModelPlugin.add_image">[docs]</a>    <span class="k">def</span> <span class="nf">add_image</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Adds image for visualization.</span>

<span class="sd">        Args:</span>
<span class="sd">            *args: TODO</span>
<span class="sd">            **kwargs: TODO</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_viz</span><span class="o">.</span><span class="n">add_image</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>

<div class="viewcode-block" id="ModelPlugin.add_histogram"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.ModelPlugin.add_histogram">[docs]</a>    <span class="k">def</span> <span class="nf">add_histogram</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Adds histogram for visualizaiton.</span>

<span class="sd">        Args:</span>
<span class="sd">            *args: TODO</span>
<span class="sd">            **kwargs: TODO</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_viz</span><span class="o">.</span><span class="n">add_histogram</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>

<div class="viewcode-block" id="ModelPlugin.add_scatter"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.ModelPlugin.add_scatter">[docs]</a>    <span class="k">def</span> <span class="nf">add_scatter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;Adds a scatter plot to visualization.</span>

<span class="sd">        Args:</span>
<span class="sd">            *args: TODO</span>
<span class="sd">            **kwargs: TODO</span>

<span class="sd">        &#39;&#39;&#39;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_viz</span><span class="o">.</span><span class="n">add_scatter</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="register_plugin"><a class="viewcode-back" href="../../cortex.html#cortex.plugins.register_plugin">[docs]</a><span class="k">def</span> <span class="nf">register_plugin</span><span class="p">(</span><span class="n">plugin</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;Registers a plugin into cortex</span>

<span class="sd">    Args:</span>
<span class="sd">        plugin: TODO</span>

<span class="sd">    Returns:</span>

<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">if</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">plugin</span><span class="p">,</span> <span class="n">ModelPlugin</span><span class="p">):</span>
        <span class="n">register_model</span><span class="p">(</span><span class="n">plugin</span><span class="p">)</span>
    <span class="k">elif</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">plugin</span><span class="p">,</span> <span class="n">DatasetPlugin</span><span class="p">):</span>
        <span class="n">register_data</span><span class="p">(</span><span class="n">plugin</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">plugin</span><span class="p">)</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, MILA.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../',
            VERSION:'',
            LANGUAGE:'None',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  <script type="text/javascript" src="../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>